





//
//  PriceTableview.m
//  住房条件帅选器
//
//  Created by MB__Lee on 2018/5/16.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "PriceTableview.h"
#import "MBNetTool.h"
#import "PriceModel.h"
#import "PriceCell.h"
#import "PriceSearchView.h"

@interface PriceTableview()<UITableViewDelegate,UITableViewDataSource,PriceSearchViewDelegate>

@property(nonatomic,strong)NSMutableArray *priceMut;


@property(nonatomic,strong)PriceSearchView *priceSearchView;

@end

static NSString *PriceCell_Identi = @"PriceCell";
static NSString *PriceSearchViewIdenti = @"PriceSearchView";

@implementation PriceTableview
-(NSMutableArray *)priceMut{
    if (!_priceMut) {
        _priceMut = [NSMutableArray array];
        
    }
    return _priceMut;
}
-(PriceSearchView *)priceSearchView{
    if (!_priceSearchView) {
        _priceSearchView = [[NSBundle mainBundle] loadNibNamed:PriceSearchViewIdenti owner:nil options:nil].lastObject;
        _priceSearchView.delegate = self;
        
        _priceSearchView.frame = CGRectMake(0, MBScaleH(25) * 4, SCREEN_WIDTH, MBScaleH(25));
        
    }
    return _priceSearchView;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        //1.从网络获取价格区间
        [self priceTableview_getRentSource];
        
        [self registerNib:[UINib nibWithNibName:PriceCell_Identi bundle:nil] forCellReuseIdentifier:PriceCell_Identi];
        self.contentInset = UIEdgeInsetsMake(0, 0, MBScaleH(50), 0);
        self.showsVerticalScrollIndicator = NO;
        self.bounces = NO;
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //添加搜索框view
        [self addSubview:self.priceSearchView];
        
        
    }
    return self;
}

#pragma mark --tableView 数据源 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.priceMut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PriceCell *cell = [tableView dequeueReusableCellWithIdentifier:PriceCell_Identi];
    cell.priceModel = self.priceMut[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  MBScaleH(25);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.ptDelegate respondsToSelector:@selector(priceTableview:sendPrice:)]) {
        
        PriceModel *model = self.priceMut[indexPath.row];
        [self.ptDelegate priceTableview:self sendPrice:model.name];
    }
}


#pragma mark --获取网络数据
-(void)priceTableview_getRentSource{
    
    NSString *category = [self getCategery];
    
    
    NSDictionary *dic =@{@"category":category,
                         @"type":@"rent",
                         @"city":MBCityCode
                         };
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [MBNetTool MBPost:Search_show paramater:dict success:^(id response) {
        if ([response[@"code"] integerValue] == 10000) {
            for (NSDictionary *priceDict in response[@"data"]) {
                PriceModel *priceModel = [[PriceModel alloc] initWithDictionary:priceDict error:nil];
                [self.priceMut addObject:priceModel];
            }
        }
        
        [self reloadData];
        
        //默认选中
        [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
    } failure:^(NSError *error) {
        
    }];
}
-(NSString *)getCategery{
    NSString *category;
    if (self.priceType == PriceTableviewTypeResidence) {
        category = @"1001";
    }else if (self.priceType == PriceTableviewTypeHotel){
        category = @"1002";
    }else if (self.priceType == PriceTableviewTypeOffBuilding){
        category = @"1003";
    }else if (self.priceType == PriceTableviewTypeShop){
        category = @"1004";
    }
    return  category;
}

#pragma mark --priceSearchView 代理方法
-(void)PriceSearchView:(PriceSearchView *)priceview sendMinPrice:(NSString *)minPrice{
    
    if ([self.ptDelegate respondsToSelector:@selector(priceTableview:sendPrice:)]) {
        [self.ptDelegate priceTableview:self sendPrice:minPrice];
    }
    
}

@end
